import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_scan_tool/bloc/tool/tool_bloc.dart';
import 'package:flutter_scan_tool/bloc/tool/tool_state.dart';
import 'package:flutter_scan_tool/models/title_count.dart';

import 'sum_item_view.dart';


class ToolSumListView extends StatefulWidget {
  final AnimationController? mainScreenAnimationController;
  final Animation<double>? mainScreenAnimation;

  const ToolSumListView(
      {Key? key, this.mainScreenAnimationController, this.mainScreenAnimation})
      : super(key: key);

  @override
  State<ToolSumListView> createState() => _ToolSumListViewState();
}

class _ToolSumListViewState extends State<ToolSumListView>
    with TickerProviderStateMixin {
  AnimationController? animationController;

  @override
  void initState() {
    animationController = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 2000));
    super.initState();
  }

  @override
  void dispose() {
    animationController?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<ToolBloc, ToolState>(
      builder: (context, state) {
        if (state is ToolLoadSuccess) {
          List<TitleCount> list = [];
          list.add(
            TitleCount('总数量', state.toolList.length, 'image/kc.jpg',
                [const Color(0xFF6F72CA), const Color(0xFF1E1466)]),
          );
          list.add(
            TitleCount(
                '在库数量',
                state.toolList.where((element) => element.state == 0).length,
                'image/rk.jpg',
                [const Color(0xFF738AE6), const Color(0xFF5F5EDD)]),
          );
          list.add(TitleCount(
              '出库数量',
              state.toolList.where((element) => element.state != 0).length,
              'image/ck.jpg',
              [Colors.blueGrey, Colors.blueAccent]));
          return AnimatedBuilder(
            animation: widget.mainScreenAnimationController!,
            builder: (BuildContext context, Widget? child) {
              return FadeTransition(
                opacity: widget.mainScreenAnimation!,
                child: Transform(
                  transform: Matrix4.translationValues(
                      0.0, 30 * (1.0 - widget.mainScreenAnimation!.value), 0.0),
                  child: Container(
                    margin: const EdgeInsets.all(20),
                    height: 400,
                    decoration: const BoxDecoration(
                      gradient: LinearGradient(
                        colors: [Colors.white70,Colors.white10],
                      ),
                      borderRadius: BorderRadius.all(Radius.circular(20)),
                    ),
                    width: double.infinity,
                    child: ListView.builder(
                      padding: const EdgeInsets.only(
                          top: 0, bottom: 0, right: 20, left: 20),
                      itemCount: list.length,
                      scrollDirection: Axis.horizontal,
                      itemBuilder: (BuildContext context, int index) {
                        final int count = list.length > 10 ? 10 : list.length;
                        final Animation<double> animation =
                            Tween<double>(begin: 0.0, end: 1.0).animate(
                                CurvedAnimation(
                                    parent: animationController!,
                                    curve: Interval((1 / count) * index, 1.0,
                                        curve: Curves.fastOutSlowIn)));
                        animationController?.forward();
                        return SumView(
                          titleCount: list[index],
                          animationController: animationController,
                          animation: animation,
                        );
                        // MealsView(
                        //   mealsListData: mealsListData[index],
                        //   animation: animation,
                        //   animationController: animationController!,
                        // );
                      },
                    ),
                  ),
                ),
              );
            },
          );
        } else if (state is ToolLoadProgress) {
          return const Center(
            child: SizedBox(
              width: 100,
              child: CircularProgressIndicator(),
            ),
          );
        } else {
          return const Center(
            child: SizedBox(
              width: 100,
              child: Text(
                '加载错误',
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ),
          );
        }
      },
    );
  }
}

// return BlocBuilder<ToolBloc, ToolState>(
// builder: (context, state) {
// // if(state is ToolLoadSuccess) {
// return AnimatedBuilder(
// animation: widget.mainScreenAnimationController!,
// builder: (BuildContext context, Widget? child) {
// List<SumView> list = [];
// list.add(
// SumView(
// title: '总数量',
// count: 15,
// imagePath: 'image/breakfast.png',
// colors: const [Color(0xFFFE95B6), Color(0xFFFF5287)],
// animation: Tween<double>(begin: 0.0, end: 1.0).animate(
// CurvedAnimation(
// parent: animationController!,
// curve: const Interval((1 / 4) * 0, 1.0,
// curve: Curves.fastOutSlowIn))),
// animationController: animationController!,
// ),
// );
// list.add(
// SumView(
// title: '在库数量',
// count: 8,
// imagePath: 'image/breakfast.png',
// colors: const [Color(0xFF738AE6), Color(0xFF5C5EDD)],
// animation: Tween<double>(begin: 0.0, end: 1.0).animate(
// CurvedAnimation(
// parent: animationController!,
// curve: const Interval((1 / 4) * 1, 1.0,
// curve: Curves.fastOutSlowIn))),
// animationController: animationController!,
// ),
// );
// list.add(SumView(
// title: '出库数量',
// count: 7,
// imagePath: 'image/breakfast.png',
// colors: const [Color(0xFFFA7D82), Color(0xFFFFB295)],
// animation: Tween<double>(begin: 0.0, end: 1.0).animate(
// CurvedAnimation(
// parent: animationController!,
// curve: const Interval((1 / 4) * 2, 1.0,
// curve: Curves.fastOutSlowIn))),
// animationController: animationController!,
// ));
// return FadeTransition(
// opacity: widget.mainScreenAnimation!,
// child: Transform(
// transform: Matrix4.translationValues(
// 0.0, 30 * (1.0 - widget.mainScreenAnimation!.value), 0.0),
// child: SizedBox(
// height: 216,
// width: double.infinity,
// child: ListView.builder(
// padding: const EdgeInsets.only(
// top: 0, bottom: 0, right: 16, left: 16),
// itemCount: list.length,
// scrollDirection: Axis.horizontal,
// itemBuilder: (BuildContext context, int index) {
// return SumView(
// colors: list[index].colors,
// imagePath: list[index].imagePath,
// animation: list[index].animation,
// animationController: list[index].animationController,
// title: list[index].title,
// count: list[index].count,
// );
// },
// ),
// ),
// ),
// );
// },
// );
// // }else if (state is ToolLoadProgress) {
// //   return const CircularProgressIndicator();
// // } else {
// //   return const Center(
// //     child: SizedBox(
// //       width: 100,
// //       child: Text(
// //         '加载错误',
// //         textAlign: TextAlign.center,
// //         style: TextStyle(
// //           fontSize: 18,
// //           fontWeight: FontWeight.w500,
// //         ),
// //       ),
// //     ),
// //   );
// // }
// },
// );
